<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

class Tools
{
	/**
	 * 格式化树形
	 * @param  array 	$menuList	数据
	 * @param  string 	$path 		标识
	 * @param  integer 	$level 		层级
	 * @return array 	$arr 		结果            
	 */
	public static function createMenuTree($menuList, $path, $level = 2)
	{
		$arr = array();
		$current_path_len = $level * 2;
		foreach ($menuList as $menu_path => $menu) {
			$t = substr($menu['menu_path'], 0, ($level-1)*2);
			if(strlen($menu['menu_path']) == $current_path_len && $t == $path) {
				$arr[$menu['menu_path']]['info'] = array(
						'id' => $menu['menu_id'],
						'name' => $menu['menu_name'],
						'path' => $menu['menu_path']
					);
				// $arr['info'] = $menu;
				unset($menuList[$menu_path]);
				$arr[$menu['menu_path']]['list'] = self::createMenuTree($menuList, $menu['menu_path'], $level+1);
				if(!$arr[$menu['menu_path']]['list'])
					unset($arr[$menu['menu_path']]['list']);
			}
		}
		return $arr;
	}

	/**
	 * 树形格式化HTML
	 * @param  array 	$menuList 	待格式化数据
	 * @return string 	$html 		格式化后的html
	 */
	public static function createMenuHtml($menuList)
	{
		$html = "";
		if($menuList) {
			$html .= "<ul>";
			foreach ($menuList as $menu) {
				$html .= "<li class=\"silu-menu\" data-path=\"{$menu['info']['path']}\" data-id=\"{$menu['info']['id']}\">";
				$html .= "<div class=\"box-tools menu-tools-{$menu['info']['path']}\">";
				foreach (C('SILU_MENU_TOOLS') as $tool) {
					$html .= "<a class=\"{$tool['icon']}\" href=\"javascript:;\" title=\"{$tool['name']}\"></a>";
				}
				$html .= "</div>";
				if(isset($menu['list']))
					$html .= "<span class=\"folder\">{$menu['info']['name']}</span>" . self::createMenuHtml($menu['list']);
				else {
					$html .= "<span class=\"file\">{$menu['info']['name']}</span>";
				}
					
				$html .= "</li>";
			}
			$html .= "</ul>";
		}
		return $html;
	}

	/**
	 * 
	 * @param  [type] $menuList [description]
	 * @return [type]           [description]
	 */
	public static function createMenuSelect($menuList, $i = 1, &$select, $current_path)
	{
		$path = substr($current_path, 0, $i *2);
		$path_tmp = substr($current_path, 0, ($i-1) *2);
		$select[$i] = "<div class=\"v1\"><select name=\"\" class=\"change-platform\" data-parent-path=\"{$path_tmp}\"><option value=\"\">请选择</option>";
		foreach ($menuList as $key => $menu) {
			$select[$i] .= "<option value=\"{$menu['info']['path']}\"" . ($menu['info']['path'] == $path ? " selected" : "") . ">{$menu['info']['name']}</option>";
			if(isset($menu['list']) && $menu['list']) {
				self::createMenuSelect($menu['list'], $i+1, $select, $current_path);
				// $select = array_merge($select, $tmp);
			}
		}
		$select[$i] .= "</select></div>";
	}

}
